package com.lzq.leetcode.hoot100;

import com.lzq.leetcode.hoot100.ds.ListNode;

import java.util.ListResourceBundle;

public class s234 {

    public static void main(String[] args) {
        ListNode head = new ListNode(new int[]{1,2});
        System.out.println(new s234().isPalindrome(head));
    }

    public boolean isPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode h2 = reverse(slow);
        while (h2 != null && head != null) {
            if (h2.val != head.val) {
                return false;
            }
            h2 = h2.next;
            head = head.next;
        }
        return true;
    }

    private ListNode reverse(ListNode head) {

        ListNode pre = head, p = head.next;

        pre.next = null;
        while (p != null) {
            ListNode temp = p.next;
            p.next = pre;
            pre = p;
            p = temp;
        }

        return pre;
    }
}
